package micropascalcomp.FSA;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import micropascalcomp.MP_Token;
import micropascalcomp.Token;

/******************************************
 * 
 * GetIdentifier
 * Takes a string as input and outputs a valid comment token
 * @author Jason Welch, Codi Gann, Jamal Martin
 * 
 *******************************************/
public class CommentsFSA implements FSABehavior {

    private char openingBracket = '{';
    private char closingBracket = '}';

    public MP_Token getToken(FileReader file, LineNumberReader inFile, int colNum) {
        char readChar;
        int col = colNum;

        try {
            inFile.mark(col);
            readChar = (char) inFile.read();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        if (readChar != openingBracket) {
            return null;
        }

        String lex = "";
        boolean ready = false;
         
        do {

            lex = lex.concat(Character.toString(readChar));
            col++;

            
            try {
                //inFile.mark(col);
                readChar = (char) inFile.read();
                ready = inFile.ready();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }

            if (!ready) {
                lex = lex.concat(Character.toString(readChar));
                col++;

                try {
                    inFile.mark(col);
                    inFile.reset();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return new MP_Token(Token.MP_RUN_COMMENT, lex, inFile.getLineNumber(), colNum);
            }
             
             

        } while (readChar != closingBracket);

        lex = lex.concat(Character.toString(readChar));
        col++;

        try {
            inFile.mark(col);
            inFile.reset();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return new MP_Token(Token.valueOf("MP_COMMENT"), lex, inFile.getLineNumber(), colNum);
    }
}//end class
